import { Product } from "@domains/product/domain/entities/Product.ts";
import { ProductType } from "@domains/product/domain/type/ProductType.ts";
import { ProductRepository } from "@domains/product/ports/repositories/ProductRepository";
import { http } from "shared/infra";
import HttpConfig from "../config/HttpConfig";


/**
 * HTTP商品仓储实现 - 实现商品数据的HTTP访问
 * 这是基础设施层，负责具体的数据获取实现
 */
export class HttpProductRepository implements ProductRepository {

    async findAll(): Promise<Array<ProductType>> {
        const result: Array<ProductType> = await http.get({ url: HttpConfig.apiUrlList.productsListUrl });
        const items = result.map((item) => Product.fromDTO(item))
        return items;
    }

    async findById(id: number): Promise<Product> {
        const result: ProductType = await http.get({
            url: HttpConfig.apiUrlList.productsInfoUrl,
            data: { id }
        });
        return Product.fromDTO(result);
    }
}